"""
-------------------------------
- File_Name: logs.py
- Date: 2025/2/17
- Author: yangzhide
- Email: xxzjyzd@126.com
- Description: 日志记录
-------------------------------
"""

from fastapi import APIRouter, Query, Depends
from starlette.requests import Request

from app.core.security import get_current_user
from app.dependencies.authDepend import oauth2_scheme
from app.schemas import BaseResponse
from app.schemas.logs import LogOut
from app.utils.common import read_log
from app.utils.responseUtil import ResponseUtil

router = APIRouter(dependencies=[Depends(oauth2_scheme),Depends(get_current_user)])
tags = ["日志"]
description = "操作日志记录"

@router.get("/logs", summary="日志列表", response_model=BaseResponse[LogOut])
async def get_logs(
        request: Request,
        page: int = Query(1, description="页码"),
        page_size: int = Query(10,ge=1,le=100,description="每页数量")
):
    # 日志文件
    file_path = "logs/app.log"
    # 读取日志
    logs = read_log(file_path, page, page_size)

    # 获取总行数
    with open(file_path,"r",encoding="utf-8") as f:
        total = sum(1 for _ in f)

    data = {"logs": logs, "total": total, "page": page, "page_size": page_size}
    return ResponseUtil.success_response(request,data,msg="查询日志记录成功")